using DbEntities.Mappings;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;

namespace DataLayer.Utils
{
    public class NHibernateSessionFactory
    {
        protected virtual Configuration Configuration { get; private set; }


        public ISessionFactory CreateSessionFactory()
        {
            Configuration = BuildConfiguration();
            ISessionFactory sessionFactory = Configuration.BuildSessionFactory();
            SchemaExport(Configuration);
            return sessionFactory;
        }

        private Configuration BuildConfiguration()
        {
            PostgreSQLConfiguration config = PostgreSQLConfiguration.Standard
                .ConnectionString(c => c
                                           .Host("localhost")
                                           .Port(5432)
                                           .Database("plumber")
                                           .Username("postgres")
                                           .Password("start"));

            FluentConfiguration configuration = Fluently
                .Configure()
                .Database(config)
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<PublisherMap>());

            return configuration.BuildConfiguration();
        }

        protected void SchemaExport(Configuration config)
        {
            // new SchemaUpdate(config).Execute(true, true);
           //   new NHibernate.Tool.hbm2ddl.SchemaExport(config).Execute(true, true, false);
        }
    }
}